/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.sowrov.afrigis.tools.timesheet;

import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * Write about the class here
 *
 * @author Mahbub Mozadded (Sowrov)
 * @date Apr 4, 2010
 */
public class User {
    private static User user = null;
    //name of the user in server
    private String name;

    //password to access server
    private String passWord;

    //server url
    private String url;

    //server port
    private String port;

    //interval time in hour
    private int interval;

    //complete project and activity list of a user
    private ArrayList<Project> projectList;

    private long lastSyncTime = 0;

    /**
     * create a user with no user name and password
     */
    private User() {
        name = "";
        passWord = "";
        url = "196.35.231.10";
        port = "80";
        projectList = new ArrayList<Project>();
    }

    /**
     * create a user with give name and password and default location url
     * @param name user name to access the site
     * @param passWord user password
     */
    private User(String name, String passWord) {
        this.name = name;
        this.passWord = passWord;
        url = "196.35.231.10";
        port = "80";
        projectList = new ArrayList<Project>();
    }

    public static User getInstance(){
        if (User.user==null) {
            User.user = new User();
        }
        return User.user;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    /**
     * Get the time interval in hour
     *
     * @return time interval
     */
    public int getInterval() {
        return interval;
    }

    /**
     * set the time interval in hour
     * 
     * @param interval time interval
     */
    public void setInterval(int interval) {
        this.interval = interval;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public long getLastSyncTime() {
        return lastSyncTime;
    }

    public void setLastSyncTime(long lastSyncTime) {
        this.lastSyncTime = lastSyncTime;
    }

    /**
     * Add a new project, it should be a fully constructed project
     * 
     * @param project user project
     */
    public void addProject(Project project) {
        this.projectList.add(project);
    }

    /**
     * remove all previous project and add only the projects in the given Arraylist
     * 
     * @param proj list of project
     * @return false if the given list is null or empty
     */
    public boolean replaceAllProject(ArrayList<Project> proj) {
        if (proj == null || proj.isEmpty()) {
            return false;
        }
        this.projectList.clear();
        for (int i=0; i<proj.size(); i++) {
            this.projectList.add(proj.get(i));
        }
        return true;
    }

    /**
     * remove all current projects
     */
    public void clearProject() {
        this.projectList.clear();
    }

    /**
     * total number of projects currently added
     * 
     * @return number of projects
     */
    public int getProjectCount() {
        return this.projectList.size();
    }

    public ArrayList<Project> getProjectList() {
        return projectList;
    }
    
    public Project getProjectById(String pId) {
        Project p = null;
        for (int i=0; i<this.projectList.size(); i++) {
            if (this.projectList.get(i).getId().equalsIgnoreCase(pId)) {
                p = this.projectList.get(i);
                break;
            }
        }
        return p;
    }

    public int getProjectIndexById (String pId) {
        int index = -1;
        for (int i=0; i<this.projectList.size(); i++) {
            if (this.projectList.get(i).getId().equalsIgnoreCase(pId)) {
                index = i;
                break;
            }
        }
        return index;
    }
    
    public Project getProjectAt(int i) {
        if (i >= this.projectList.size()) {
            return null;
        }
        return this.projectList.get(i);
    }

    public void removeProjectAt(int i) {
        if (i<this.projectList.size()) {
            this.projectList.remove(i);
        }
    }

    public void initUserWithDummyInfo() {
        Project tmpPro = new Project("Bangladesh Office Meal", "1170/0");
        Activity tmpAct = new Activity("Lunch", "1115", false);
        tmpPro.addActivity(tmpAct);
        this.addProject(tmpPro);

        tmpPro = new Project("Leisure activities", "1171/0");
        tmpAct = new Activity("Recreation", "1117", false);
        tmpPro.addActivity(tmpAct);
        this.addProject(tmpPro);

        tmpPro = new Project("In Lieu Holiday", "1175/0");
        tmpAct = new Activity("Public holiday exchange", "1119", false);
        tmpPro.addActivity(tmpAct);
        tmpAct = new Activity("Weekend exchange", "1118", false);
        tmpPro.addActivity(tmpAct);
        this.addProject(tmpPro);

        tmpPro = new Project("No Work", "1184/0");
        tmpAct = new Activity("No Work", "1120", false);
        tmpPro.addActivity(tmpAct);
        this.addProject(tmpPro);

        tmpPro = new Project("AfriGIS Leave and Performance Discussions", "560/1");
        tmpAct = this.parseActivityOpt("'95','','Annual Leave'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'98','','HR '");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'103','','Office Meetings & Socials'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'137','','Performance Discussion'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'144','','Public Holiday'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'99','','Research'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'100','','Sick Leave'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'101','','Special Leave'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'106','','Training'");
        tmpPro.addActivity(tmpAct);
        this.addProject(tmpPro);

        tmpPro = new Project("iSearch Project", "1049/250");
        tmpAct = this.parseActivityOpt("'43','','Bug Fixing'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'45','','Code Reviews'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'46','','Coding'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'47','','Database Design'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'49','','Database Maintenance'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'51','','Design'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'52','','Documentation'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'59','','Functional Requirements'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'60','','Functional Spec Review'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'61','','Functional Specification'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'142','','IT Support'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'69','','Project Communication'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'64','','Project Documentation'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'136','','Project Meeting'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'65','','Project Resource Management'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'67','','Project Time Management'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'77','','Developer Testing'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'75','','Installation Testing'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'79','','Test Plans'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'78','','Testing'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'85','','e-mail Support'");
        tmpPro.addActivity(tmpAct);
        tmpAct = this.parseActivityOpt("'81','','Manuals'");
        tmpPro.addActivity(tmpAct);
        this.addProject(tmpPro);

    }

    Activity parseActivityOpt(String str) {
        Activity act = new Activity();
        StringTokenizer strtok = new StringTokenizer(str, "',");
        act.setId(strtok.nextToken());
        act.setName(strtok.nextToken());
        act.setBillAble(true);
        return act;
    }

    public static void main(String arg[]) {
        User p = new User("Mahbub", "Sowrov");
        p.initUserWithDummyInfo();
        //p.write();
    }
}
